package gateway.config.security.strategy;

import common.model.enums.ApplicationRunnerOrderEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * 将SecurityContextHolder的储存策略设置为MODE_INHERITABLETHREADLOCAL，子线程可以获取上下文对象
 *
 * @author 米泽鹏
 * @since 2022/12/29 上午 11:15
 */
@Component
@Slf4j
public class SecurityStrategyConfig implements ApplicationRunner, Ordered {
	@Override
	public void run(ApplicationArguments args) {
		SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
		log.info("设置SecurityContextHolder储存策略为：" + SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
	}

	@Override
	public int getOrder() {
		return ApplicationRunnerOrderEnum.SECURITY_STRATEGY_CONFIG.getOrder();
	}
}
